package es.uned.hoomgle.hibernate.beans.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;

import org.apache.solr.analysis.LowerCaseFilterFactory;
import org.apache.solr.analysis.SnowballPorterFilterFactory;
import org.apache.solr.analysis.StandardTokenizerFactory;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Parameter;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;

import es.uned.hoomgle.hibernate.beans.interfaces.CityI;
import es.uned.hoomgle.hibernate.beans.interfaces.HomeI;
import es.uned.hoomgle.hibernate.beans.interfaces.UserI;

@Entity
@AnalyzerDef(name = "customanalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = {
		@TokenFilterDef(factory = LowerCaseFilterFactory.class),
		@TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
				@Parameter(name = "language", value = "Spanish")
		})
})
@Indexed
public class Home implements HomeI{
	/**
	 * @uml.property  name="id"
	 */
	@Id
	@DocumentId
	@Column
	@GeneratedValue
	private Integer id;
	
	@Column
	@Field
	private Integer area;
	
	@ManyToOne
	@IndexedEmbedded
	private City city;
	/**
	 * @uml.property  name="cost"
	 */
	@Column
	@Field
	private Integer cost;
	/**
	 * @uml.property  name="hasGarden"
	 */
	@Column
	@Field
	private boolean hasGarden;
	
	@Column(length=1000) 
	@Field
	private String description;
	
	@Lob @Column
	private byte[] picture;
	
	@ManyToOne(cascade=CascadeType.ALL)
	@IndexedEmbedded
	@OnDelete(action = OnDeleteAction.CASCADE) 
	@Cascade(org.hibernate.annotations.CascadeType.DELETE) 
	private User user;
	
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#getId()
	 */
	/**
	 * @return
	 * @uml.property  name="id"
	 */
	@Override
	public Integer getId() {
		return id;
	}
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#setId()
	 */
	/**
	 * @param  id
	 * @uml.property  name="id"
	 */
	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#getarea()
	 */
	public Integer getArea() {
		return area;
	}
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#setarea()
	 */
	public void setArea(Integer area) {
		this.area = area;
	}
	
	public City getCity() {
		return city;
	}

	public void setCity(CityI city) {
		if(city instanceof City)
			this.city = (City)city;
	}
	
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#getCost()
	 */
	/**
	 * @return
	 * @uml.property  name="cost"
	 */
	@Override
	public Integer getCost() {
		return cost;
	}
	
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#setCost()
	 */
	/**
	 * @param  cost
	 * @uml.property  name="cost"
	 */
	@Override
	public void setCost(Integer cost) {
		this.cost = cost;
	}
	
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#isHasGarden()
	 */
	/**
	 * @return
	 * @uml.property  name="hasGarden"
	 */
	@Override
	public boolean isHasGarden() {
		return hasGarden;
	}

	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#setHasGarden()
	 */
	/**
	 * @param  hasGarden
	 * @uml.property  name="hasGarden"
	 */
	@Override
	public void setHasGarden(boolean hasGarden) {
		this.hasGarden = hasGarden;
	}
	
	
	@Override
	public UserI getUser() {
		return user;
	}
	
	@Override
	public void setUser(UserI user) {
		if(user instanceof User){
			this.user = (User)user;
		}		
	}
	
	
	
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	
	public byte[] getPicture() {
		return picture;
	}
	public void setPicture(byte[] picture) {
		this.picture = picture;
	}
	/* (non-Javadoc)
	 * @see es.uned.hoomgle.hibernate.beans.impl.HomeI#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Home [getId()=");
		builder.append(getId());
		builder.append(", hasGarden=");
		builder.append(isHasGarden());
		builder.append(", getCost=");
		builder.append(getCost());
		builder.append(", getUser=");
		builder.append(getUser());
		builder.append("]");
		return builder.toString();
	}
	
}
